﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;
using System.ComponentModel;
using System.ComponentModel.Composition;

namespace Steve.UI.Extensions
{
    [Export(typeof(IConfiguration))]
    [PartCreationPolicy(CreationPolicy.Shared)]
    public sealed class AppSettingsConfiguration : DefaultConfiguration
    {
        public override string MembershipIdCookieName
        {
            get
            {
                var config = ConfigurationManager.AppSettings["MembershipIdCookieName"];
                if (config == null) return base.MembershipIdCookieName;
                return config;
            }
        }

        public override TimeSpan DataCacheTtl
        {
            get
            {
                TimeSpan value;
                var config = ConfigurationManager.AppSettings["DataCacheTtl"];
                if (config == null || !TimeSpan.TryParse(config, out value)) return base.DataCacheTtl;
                return value;                
            }
        }

        public override string[] SearchStopWords
        {
            get
            {
                string value = ConfigurationManager.AppSettings["SearchStopWords"];

                if (string.IsNullOrWhiteSpace(value))
                {
                    return base.SearchStopWords;
                }

                return value.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
            }
        }

        public override string WorkingDirectory
        {
            get
            {
                var config = ConfigurationManager.AppSettings["WorkingDirectory"];
                if (config == null) return base.WorkingDirectory;
                return config;
            }
        }
    }
}
